package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.MaterialCertificationRules;
import com.pureut.system.domain.dto.MaterialCertificationRulesDto;
import com.pureut.system.domain.vo.MaterialCertificationRulesVo;
import com.pureut.system.mapper.MaterialCertificationRulesMapper;
import com.pureut.system.service.IMaterialCertificationRulesService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import javax.annotation.Resource;

/**
 * @Author: pureut
 * @Date: 2024-12-04 13:04:04
 * @Desc: 物料认证规则
 */
@Service("materialCertificationRulesService")
public class MaterialCertificationRulesServiceImpl extends ServiceImpl<MaterialCertificationRulesMapper, MaterialCertificationRules> implements IMaterialCertificationRulesService {

    /**
     * 物料认证规则Dao
     */
    @Resource
    private MaterialCertificationRulesMapper materialCertificationRulesMapper;

    /**
     * 查询列表
     * @param materialCertificationRulesVo
     * @return
     */
    @Override
    public List<MaterialCertificationRulesDto> queryPage(MaterialCertificationRulesVo materialCertificationRulesVo) {
        // 获取认证方式的数据字典
        List<SysDictData> netArray = DictUtils.getDictCache("authentication_method");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        // 获取物料等级的数据字典
        List<SysDictData> levelArray = DictUtils.getDictCache("material_grade");
        Map<String, String> levelMap = levelArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        // 总数、结果
        List<MaterialCertificationRulesDto> materialCertificationRulesDtos = materialCertificationRulesMapper.selectByPage(materialCertificationRulesVo);
        materialCertificationRulesDtos.forEach(e->{
            e.setAuthenticationMode(netMap.get(String.valueOf(e.getAuthenticationMode())));
            e.setMaterialGrade(levelMap.get(String.valueOf(e.getMaterialGrade())));
        });
        return materialCertificationRulesDtos;
    }


    /**
     * 新增物料认证规则
     * @param materialCertificationRulesVo
     * @return
     */
    @Override
    public Boolean save(MaterialCertificationRulesVo materialCertificationRulesVo) {
        MaterialCertificationRules materialCertificationRules = new MaterialCertificationRules();
        BeanUtil.copyProperties(materialCertificationRulesVo, materialCertificationRules);
        materialCertificationRules.setCreateBy(SecurityUtils.getUsername());
        materialCertificationRules.setCreateTime(new Date());
        return materialCertificationRulesMapper.insert(materialCertificationRules)> 0;
    }


    /**
     * 更新物料认证规则
     * @param materialCertificationRulesVo
     * @return
     */
    @Override
    public Boolean updateById(MaterialCertificationRulesVo materialCertificationRulesVo) {
        MaterialCertificationRules materialCertificationRules = new MaterialCertificationRules();
        BeanUtil.copyProperties(materialCertificationRulesVo, materialCertificationRules);
        materialCertificationRules.setUpdateBy(SecurityUtils.getUsername());
        materialCertificationRules.setUpdateTime(new Date());
        return updateById(materialCertificationRules);
    }
}
